﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace 注册列表操作
{
    class Program
    {
        const string keyName = "CF30BackGroundSoftware";
        const string regListPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
        const string folderName = "Run";
        //softwareStartPath
        const string valueName = "D:\\testshare\\Debug\\CF30BackGroundSoftware.exe";

        static void Main(string[] args)
        {
            //string registData = GetRegistData(keyName);

            CreateKey("AANiRen");
            WriteRegedit("AANiRen", keyName, valueName);
            object obj = ReadRegedit("AANiRen", keyName);
            string path = obj as string;
            Debug.WriteLine("读出的数据为：" + path);
            DeleteKey("AANiRen");
            //判断存在性
            if (GetRegistData(keyName) != "")
            {
                Console.WriteLine(keyName + " 已存在！");
                Console.ReadKey();
                return;
            }

            Console.WriteLine(keyName + " 不存在，准备写入！");

            //不存在
            WTRegedit(keyName, valueName);

            Console.WriteLine(keyName + " 写入完成，下面进行读出，判断写入是否正确！");

            if (GetRegistData(keyName) != "")
            {
                Console.WriteLine(keyName + " 已写入！");
            }
            else
            {
                Console.WriteLine(keyName + " 写入不成功！");
            }
            //Console.WriteLine(keyName + " 请输入任意键结束！");
            //Console.ReadKey();
        }

        /// <summary>
        /// 写注册列表
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="regValue"></param>
        /// <param name="startPath">软件的启动路径</param>
        /// <returns></returns>
        static public bool WriteRegList(string regKey,string regValue,string startPath)
        {
            if (GetRegistData(regKey) != "")
            {
                return true;
            }

            //不存在
            WTRegedit(regKey, regValue);

            if (GetRegistData(regKey) != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //读取指定名称的注册表的值
        static private string GetRegistData(string name)
        {
            string registData = "";
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            try
            {
                registData = aimdir.GetValue(name).ToString();
            }
            catch { }

            return registData;
        }

        static private void CreateKey(string keyName)
        {
            //RegistryKey key = Registry.LocalMachine;
            RegistryKey key = Registry.CurrentUser;
            RegistryKey software = key.CreateSubKey(@"SOFTWARE\" + keyName);
        }

        static private void DeleteKey(string keyName)
        {
            RegistryKey key = Registry.CurrentUser;
            try {
                key.DeleteSubKey(@"SOFTWARE\" + keyName);
            } catch { }
        }

        static private void DeleteSubKey(string name)
        {
            RegistryKey key = Registry.CurrentUser;
            RegistryKey subKey = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            subKey.DeleteSubKey("NRBackgroundSoft");
        }

        static private void WriteRegedit(string keyName,string name,string tovalue)
        {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"SOFTWARE\" + keyName, true);
            software.SetValue(name, tovalue);
        }

        static private object ReadRegedit(string keyName,string name)
        {
            RegistryKey hklm = Registry.CurrentUser;
            RegistryKey software = hklm.OpenSubKey(@"SOFTWARE\" + keyName, true);
            if (software == null) return null;
            return software.GetValue(name);
        }

        ///向注册表中写数据 
        static private void WTRegedit(string name, string tovalue)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.CreateSubKey(folderName);
            aimdir.SetValue(name, tovalue);
        }

        ///删除注册表中指定的注册表项 
        static private void DeleteRegist(string name)
        {
            string[] aimnames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            aimnames = aimdir.GetSubKeyNames();
            foreach (string aimKey in aimnames)
            {
                if (aimKey == name)
                    aimdir.DeleteSubKeyTree(name);
            }
        }

        //判断指定注册表项是否存在 ,这个函数有问题，所以用 GetRegistData 函数是否能读出键名来代替
        static private bool IsRegeditExit(string name)
        {
            bool _exit = false;
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(regListPath, true);
            RegistryKey aimdir = software.OpenSubKey(folderName, true);
            subkeyNames = aimdir.GetSubKeyNames();
            Console.WriteLine("读出的数组长度：" + subkeyNames.Length);
            foreach (string key in subkeyNames)
            {
                Console.WriteLine("在判断函数里打印读到的键值:" + key);
                if (key == name)
                {
                    _exit = true;
                    return _exit;
                }
            }
            return _exit;
        }
    }
}
